<!-- login.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>留言板登录界面</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
        }
        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        .login-container h2 {
            margin-bottom: 20px;
        }
        .login-container input[type="text"],
        .login-container input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .login-container input[type="checkbox"] {
            margin-right: 5px;
        }
        .login-container input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .login-container input[type="submit"]:hover {
            background-color: #0056b3;
        }
        .error-message {
            color: red;
            margin-top: 10px;
        }
    </style>
    <script>
        function encryptData(data) {
            // 使用 Base64 编码进行简单的加密
            return btoa(data);
        }

        function handleSubmit(event) {
            event.preventDefault(); // 阻止表单默认提交行为

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const rememberMe = document.getElementById('rememberMe').checked ? 'on' : '';

            // 打印和显示获取到的用户名和密码
            console.log("Username:", username);
            console.log("Password:", password);

            // 加密用户名和密码
            const encryptedUsername = encryptData(username);
            const encryptedPassword = encryptData(password);

            console.log("Encrypted Username:", encryptedUsername);
            console.log("Encrypted Password:", encryptedPassword);

            // 创建隐藏字段来存储加密后的数据
            const form = document.getElementById('loginForm');

            // 创建 hidden 字段
            const hiddenUsername = document.createElement('input');
            hiddenUsername.type = 'hidden';
            hiddenUsername.name = 'encryptedUsername';
            hiddenUsername.value = encryptedUsername;

            const hiddenPassword = document.createElement('input');
            hiddenPassword.type = 'hidden';
            hiddenPassword.name = 'encryptedPassword';
            hiddenPassword.value = encryptedPassword;

            const hiddenRememberMe = document.createElement('input');
            hiddenRememberMe.type = 'hidden';
            hiddenRememberMe.name = 'rememberMe';
            hiddenRememberMe.value = rememberMe;

            // 将隐藏字段添加到表单中
            form.appendChild(hiddenUsername);
            form.appendChild(hiddenPassword);
            form.appendChild(hiddenRememberMe);

            // 打印表单内容以验证隐藏字段是否添加成功
            console.log("Form HTML:", form.innerHTML);
            // 提交表单
            form.submit();
        }
    </script>
</head>
<body>
<div class="login-container">
    <h2>留言板登录界面</h2>
    <form id="loginForm" action="login" method="post" onsubmit="handleSubmit(event)">
        用户名: <input type="text" id="username" name="username" value="${username}" required><br><br>
        密码: <input type="password" id="password" name="password" value="${password}" required><br><br>
        <input type="checkbox" id="rememberMe" name="rememberMe" ${rememberMe}> 记住我<br><br>
        <input type="submit" value="登录">
    </form>
    <c:if test="${not empty error}">
        <p class="error-message">${error}</p>
    </c:if>
</div>
</body>
</html>
